package hn.cch.spring.boot.configuration;

import hn.cch.spring.boot.filter.HandlerInterceptorImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.TaskExecutor;
import org.springframework.web.servlet.config.annotation.AsyncSupportConfigurer;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.concurrent.TimeUnit;

@Configuration
public class WebMvcConfiguration implements WebMvcConfigurer {

    @Autowired
    public TaskExecutor taskExecutor;

    @Override
    public void configureAsyncSupport(AsyncSupportConfigurer configurer) {
        configurer.setDefaultTimeout(TimeUnit.SECONDS.toMillis(60));
    }

    /**
     * 添加拦截器
     *
     * @param interceptorRegistry
     */
    @Override
    public void addInterceptors(InterceptorRegistry interceptorRegistry) {
        interceptorRegistry.addInterceptor(new HandlerInterceptorImpl());
    }


    /**
     * 添加跨域请求配置
     *
     * @param corsRegistry
     */
    @Override
    public void addCorsMappings(CorsRegistry corsRegistry) {

        corsRegistry
                // 设置允许跨域请求的路径
                .addMapping("/**")
                // 设置允许跨域请求的域名
                // .allowedOriginPatterns("*")
                // 设置允许跨域请求的method方式
                .allowedMethods("GET", "POST", "DELETE", "PUT")
                // 设置允许跨域请求的header属性
                .allowedHeaders("*")
                // 是否允许cookie
                .allowCredentials(true)
                // 跨域允许时间
                .maxAge(3600);
    }
}
